package com.taobao.me.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.taobao.base.model.RestResponse;
import com.taobao.me.mapper.AddressMapper;
import com.taobao.me.model.dto.AddressDto;
import com.taobao.me.model.dto.AddressPreview;
import com.taobao.me.model.po.Address;
import com.taobao.me.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author erebus
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {


    @Autowired
    private AddressMapper addressMapper;


    @Override
    public RestResponse<List<AddressDto>> getAddressById(int userId) {
        // 拼接查询条件
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        // 根据用户id查询
        queryWrapper.eq(Address::getUserId, userId);
        // 执行查询
        List<Address> addresses = addressMapper.selectList(queryWrapper);
        List<AddressDto> addressDtos = new ArrayList<>();
        if (addresses != null) {
            for (Address address : addresses) {
                AddressDto addressDto = new AddressDto();
                BeanUtils.copyProperties(address, addressDto);
                addressDtos.add(addressDto);
            }

            return RestResponse.success(addressDtos);
        }

        return RestResponse.validfail("地址id不能为空");
    }

    @Override
    public RestResponse<String> insertAddress(int userId, AddressDto addressDto) {

        if (addressDto != null) {
            Address address = new Address();
            BeanUtils.copyProperties(addressDto, address);
            address.setUserId(userId);
            address.setCreateTime(LocalDateTime.now());
            address.setUpdateTime(LocalDateTime.now());
            int i = addressMapper.insert(address);
            if (i > 0) {
                return RestResponse.success("添加成功");
            }
        }
        return RestResponse.validfail("添加失败");
    }

    @Override
    public RestResponse<String> deleteAddress(int addressId) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        // 根据addressId删除
        queryWrapper.eq(Address::getAddressId, addressId);
        int delete = addressMapper.delete(queryWrapper);
        if (delete > 0) {
            return RestResponse.success("删除成功");
        }
        return RestResponse.validfail("删除失败");
    }

    @Override
    @Transactional
    public RestResponse<String> updateAddress(AddressDto addressDto) {

        if (addressDto != null) {
            Address address = new Address();
            BeanUtils.copyProperties(addressDto, address);
            this.deleteAddress(addressDto.getAddressId());
            address.setUpdateTime(LocalDateTime.now());
            int i = this.addressMapper.insert(address);
            if (i > 0) {
                return RestResponse.success("修改成功");
            }
        }
        return RestResponse.validfail("修改失败");
    }

    @Override
    public RestResponse<List<AddressPreview>> getAddressPreview(int userId) {
        // 拼接查询条件
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        // 根据用户id查询
        queryWrapper.eq(Address::getUserId, userId);
        // 执行查询
        List<Address> addresses = addressMapper.selectList(queryWrapper);
        List<AddressPreview> addressPreviews = new ArrayList<>();
        if (addresses != null) {
            for (Address address : addresses) {
                AddressPreview addressPreview = new AddressPreview();
                addressPreview.setName(address.getContactName());
                addressPreview.setPhone(address.getPhoneNumber());
                addressPreview.setAddress(address.getProvince() + address.getCity() + address.getDistrict() + address.getDetailedAddress());
                addressPreviews.add(addressPreview);
            }

            return RestResponse.success(addressPreviews);
        }

        return null;
    }


}
